package com.iot.nev.gateway.connector.mysql;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 测试MySQL读写性能
 * @author Administrator
 *
 */
public class TestMysqlRW {
	public static void main(String[] args) {
		try {
			MysqlConnector mysqlConnector = new MysqlConnector();
			Connection con = mysqlConnector.getConnection();
			Statement smt = con.createStatement();
			long duration = 0;
			int i = 1;
			long s = System.currentTimeMillis();
			do {
				// save(smt, i++);
				get(smt, i++);
				long e = System.currentTimeMillis();
				duration = e - s;
			} while (i < 10000);
			System.err.println(duration + "ms, 共：" + i);
			mysqlConnector.close();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
	
	public static void save(Statement smt, int i){
		try {
			// 要执行的SQL语句
			String sql = "INSERT INTO `vehtest` VALUES ('"+ (i) +"','3', '5', '1', '5466', '9999', '2645', '6355', '12', '13', '14', '42345', '15', '2', '12', '3', '210', '32345', '24567', '187', '45678', '284955', '2', '227', '54789', '24578', '168', '54678', '192', '7000', '3112', '32132', '22442', '151', '11', '10450', '23', '11223', '52', '52', '16', '100', '3800', '5', '5', '5', '68', '69', '14780', '88', '89', '12146', '44', '45', '151', '25', '26', '15', '2', '65535', '7', '7', '7', '7', '1', '1', '8888', '88', '21', '54', '78', '1', '221', '55871')";
			// 3.ResultSet类，用来存放获取的结果集！！
			smt.executeUpdate(sql);
		} catch (SQLException e) {
			// 数据库连接失败异常处理
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void get(Statement smt, int i){
		try {
			// 要执行的SQL语句
			String sql = "select * from vehtest where id=1";
			// 3.ResultSet类，用来存放获取的结果集！！
			ResultSet executeQuery = smt.executeQuery(sql);
			// ResultSet executeQuery = smt.executeQuery(sql);
			// executeQuery.next();
			// System.out.println(executeQuery.getString(2));
			System.out.println(i + ": " + executeQuery.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
